package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.RegisterBean;
import cst.a931xmc.yuejian20.Bean.UserBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.UserModel;
import cst.a931xmc.yuejian20.R;
import de.hdodenhof.circleimageview.CircleImageView;

public class UpdateInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private int user_id;
    private String user_pwd;
    private Context context;
    private CircleImageView user_pic;
    private EditText user_name;
    private EditText user_password;
    private EditText user_tel;
    private RadioButton rb_male;
    private RadioButton rb_female;
    private String user_sex;
    private EditText user_province;
    private EditText user_city;
    private EditText user_intro;
    private ImageButton btn_back;
    private Button btn_update;
    private UserModel model;
    private Listener<UserBean> listener;
    private Listener<RegisterBean> listener1;
    private String user_tel1;
    private String user_tel2;
    private String user_pwd2;
    SharedPreferences sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updateinfo);
        init();
        initData();
        model = new UserModel();
        model.getUserDetail(user_id,listener);
    }

    public void init(){
        user_pic = findViewById(R.id.user_pic);
        user_name = findViewById(R.id.user_name);
        user_password = findViewById(R.id.user_password);
        user_tel = findViewById(R.id.user_tel);
        rb_male = findViewById(R.id.rb_male);
        rb_female = findViewById(R.id.rb_female);
        user_province = findViewById(R.id.user_province);
        user_city = findViewById(R.id.user_city);
        user_intro = findViewById(R.id.user_intro);
        btn_back = findViewById(R.id.btn_back);
        btn_update = findViewById(R.id.btn_update);
        btn_back.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id",0);
        user_pwd = sp.getString("user_password","");
    }

    public void initData(){
        listener = new Listener<UserBean>() {
            @Override
            public void onResponse(UserBean l) {
                if(l.getUser_id()!=0){
                    user_name.setText(l.getUser_name());
                    user_password.setText(user_pwd);
                    user_tel.setText(l.getUser_tel());
                    user_tel1=l.getUser_tel();
                    user_province.setText(l.getUser_province());
                    user_city.setText(l.getUser_city());
                    user_intro.setText(l.getUser_intro());
                    if(l.getUser_sex().equals("男")) {
                        rb_male.setChecked(true);
                        rb_female.setChecked(false);
                    }
                    else if(l.getUser_sex().equals("女")) {
                        rb_female.setChecked(true);
                        rb_male.setChecked(false);
                    }
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/posters/" + l.getUser_pic())
                            .placeholder(R.drawable.nopic)
                            .into(user_pic);
                }
                else {
                    Toast.makeText(UpdateInfoActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(UpdateInfoActivity.this,message,Toast.LENGTH_LONG).show();
            }
        };

        listener1 = new Listener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean l) {
                if(!l.getSuccess().equals("0")) {
                    if(user_tel1.equals(user_tel2)&&user_pwd.equals(user_pwd2)) {
                        Intent intent = new Intent(UpdateInfoActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                        Toast.makeText(UpdateInfoActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                    }
                    else{
                        Intent intent = new Intent(UpdateInfoActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                        Toast.makeText(UpdateInfoActivity.this, "修改成功且需要重新登录", Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    Toast.makeText(UpdateInfoActivity.this,"该手机号已存在",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(UpdateInfoActivity.this,message,Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_update:
                if(rb_male.isChecked())
                    user_sex = "男";
                else if(rb_female.isChecked())
                    user_sex = "女";
                model.updateUserInfo(user_id,user_name.getText().toString(),
                                    user_password.getText().toString(),
                                    user_tel.getText().toString(),
                                    user_sex, user_province.getText().toString(),
                                    user_city.getText().toString(),
                                    user_intro.getText().toString(),listener1);
                user_tel2=user_tel.getText().toString();
                user_pwd2=user_password.getText().toString();
        }
    }
}
